<? session_start();
include 'constants.php'; 
include 'functions.php';

$employee = urldecode($_SESSION['employee_id']);
$db=new SQLiteDatabase("db/mandi");
$valid_employee = is_valid_employee($db, $employee);

if($_POST){
	if($valid_employee){
		$driver = sanitize(urldecode($_POST['driver']));
		$vehicle_num = sanitize(urldecode($_POST['vehicle_num']));
		$vehicle_type = sanitize(urldecode($_POST['vehicle_type']));
		$product = sanitize(urldecode($_POST['product']));
		$weight = sanitize(urldecode($_POST['weight']));
		$org_name = sanitize(urldecode($_POST['org_name']));
		$bearer_license = sanitize(urldecode($_POST['bearer_license']));
		$comm_agent_license = sanitize(urldecode($_POST['comm_agent_license']));
		$fees = sanitize(urldecode($_POST['fees']));
		
		$timestamp = time();
		$entry_id = $employee.'.'.$timestamp;
		
		$error_msg;
		$query = sanitize('INSERT INTO entry_table VALUES ('.
			$employee.', "'.$entry_id.'", '.$timestamp.', "'.$driver.'", '.$vehicle_num.', "'.
			$vehicle_type.'", '.$product.', '.$weight.', "'.$org_name.'", '.
			$bearer_license.', '.$comm_agent_license.', '.$fees.')');

		if(@$db->queryExec($query, $error_msg)){
			//success! erase POST variables so a new entry can be made
			$success_msg = true;
			unset($_POST);
		} else {
			//write error message further down in doc
		}
	} else {
		echo json_encode('invalid employee id');
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Mandi Entry Form</title>

</head>

<body>
<script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/event/event-min.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/connection/connection_core-min.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/json/json-min.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/dom/dom-min.js"></script>

<!-- Autocomplete -->
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.9.0/build/autocomplete/assets/skins/sam/autocomplete.css">
<script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/datasource/datasource-min.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/autocomplete/autocomplete-min.js"></script>
<?
include 'navbar.php';
$employee_id = $_SESSION['employee_id'];
$fees = ($_POST)? $_POST['fees']: '0'; ?>
<div class="body-content">
<h2>Mandi Entry Form</h2>
<? if($valid_employee) {
	if($success_msg){ echo '<p>Entry Pass successful.</p>'; }
	if($error_msg) { echo '<p>'.$error_msg.'</p>'; }
?>
	<form action="" method="POST" id="entry_form">
		<input type="hidden" name="employee_id" value="<?=$employee_id?>"></input>
		<p>Bearer License: <input id="input_bearer" type="text" name="bearer_license" value="<?=$_POST['bearer_license']?>"></input></p>
		<p>Organization Name: <input id="input_org" type="text" name="org_name" value="<?=$_POST['org_name']?>"></input></p>
		<p>Commission Agent License: <input id="input_commagent" type="text" name="comm_agent_license" value="<?=$_POST['comm_agent_license']?>"></input></p>
		<p>Driver Name: <input type="text" name="driver" value="<?=$_POST['driver']?>"></input></p>
		<p>Vehicle #: <input type="text" name="vehicle_num" value="<?=$_POST['vehicle_num']?>"></input></p>
		<p>Vehicle Type: <input type="text" name="vehicle_type" value="<?=$_POST['vehicle_type']?>"></input></p>
		<div id="myAutoComplete">
			<p>Product: <input id="input_product_name" name="product_name" value="<?=$_POST['product_name']?>"></input>
			<input id="input_product" type="hidden" name="product" value="<?=$_POST['product']?>"></input></p>
			<div id="myContainer" style="display:none;"></div>
		</div>
		<p>Weight: <input type="text" name="weight" value="<?=$_POST['weight']?>"></input></p>
		<p>Fees: <input type="text" name="fees" value="<?=$_POST['fees']?>"></input></p>
		<input type="submit"></input>
	</form>


<?
} else {
	echo '<p>Invalid employee id.</p>';
}
?>



<script type="text/javascript">
function IsNumeric(input){
   return (input - 0) == input && input.length > 0;
}

var YU = YAHOO.util;
YU.Event.addListener('input_bearer', 'focusout', function (e){
	var value = e.target.value;
	if(value != '' && IsNumeric(value)){
		var url = '<?=BASE_URL?>webservice?action=license&type=seller&id='+value;
		var callback = {
			success: function(o) {
				var results = YAHOO.lang.JSON.parse(o.responseText);
				
				var name = YU.Dom.get('bearer-name');
				var addr = YU.Dom.get('bearer-addr');
				var wrapper = YU.Dom.get('bearer-details');
				
				if(!wrapper){
					var name = document.createElement('div');
						YU.Dom.addClass(name, 'name');
						name.id = 'bearer-name';
					var addr = document.createElement('div');
						YU.Dom.addClass(addr, 'addr');
						addr.id = 'bearer-addr';
					var wrapper = document.createElement('div');
						YU.Dom.addClass(wrapper, 'bearer-details details');
						wrapper.id = 'bearer-details';
				}
				
				name.innerHTML = results['name'];
				addr.innerHTML = results['addr_street']+' '+
					results['addr_state']+' '+results['addr_pincode'];
				wrapper.appendChild(name);
				wrapper.appendChild(addr);
				YU.Dom.insertAfter(wrapper, YU.Dom.get('input_bearer'));
				YU.Dom.get('input_org').value = results['organization_name'];
			}
		};
		YU.Connect.asyncRequest('GET', url, callback);
	}
});

YU.Event.addListener('input_commagent', 'focusout', function (e){
	var value = e.target.value;
	if(value != '' && IsNumeric(value)){
		var url = '<?=BASE_URL?>webservice?action=license&type=buyer&id='+value;
		var callback = {
			success: function(o) {
				var results = YAHOO.lang.JSON.parse(o.responseText);
				
				var name = YU.Dom.get('commagent-name');
				var addr = YU.Dom.get('commagent-addr');
				var wrapper = YU.Dom.get('commagent-details');
				
				if(!wrapper){
					var name = document.createElement('div');
						YU.Dom.addClass(name, 'name');
						name.id = 'commagent-name';
					var addr = document.createElement('div');
						YU.Dom.addClass(addr, 'addr');
						addr.id = 'commagent-addr';
					var wrapper = document.createElement('div');
						YU.Dom.addClass(wrapper, 'commagent-details details');
						wrapper.id = 'commagent-details';
				}
				
				name.innerHTML = results['name'];
				addr.innerHTML = results['addr_street']+' '+
					results['addr_state']+' '+results['addr_pincode'];
				wrapper.appendChild(name);
				wrapper.appendChild(addr);
				YU.Dom.insertAfter(wrapper, YU.Dom.get('input_commagent'));
				//YU.Dom.get('input_org').value = results['organization_name'];
			}
		};
		YU.Connect.asyncRequest('GET', url, callback);
	}
});

var products;
var productsCallback = {
	success: function(o) {
		products = YAHOO.lang.JSON.parse(o.responseText);
		var product_keys = [];
		for(var key in products){
			product_keys.push(key);
		}

		var oDS = new YAHOO.util.LocalDataSource(products);
		var myAutoComp = new YAHOO.widget.AutoComplete("input_product_name","myContainer", new YAHOO.util.LocalDataSource(product_keys));
		myAutoComp.typeAhead = true;
		myAutoComp.typeAheadDelay = 0;
		myAutoComp.queryDelay = 0;
	}
};
var pUrl = '<?=BASE_URL?>'+'webservice?action=getproducts';
YU.Connect.asyncRequest('GET', pUrl, productsCallback);

YU.Event.addListener('input_product_name', 'focusout', function (e){
	var value = e.target.value;
	if(value != ''){
		YU.Dom.get('input_product').value = products[value];
	}
});

var autoselect = function(e) {
	console.log('here');
	e.target.select();
};

YU.Event.addListener('entry_form', 'focusin', autoselect);

</script>
</div>
</body>

</html>